package com.yonyougov.devops.cruxcisupport.app;

import com.yonyougov.devops.cruxcisupport.app.dto.*;
import com.yonyougov.devops.cruxcisupport.app.vo.LinkAppVo;
import com.yonyougov.devops.cruxcisupport.app.vo.NoLinkAppByRoleIdDto;
import com.yonyougov.devops.cruxcisupport.app.vo.NoLinkAppVo;
import com.yonyougov.devops.fwc.api.dto.BaseResponseDto;
import com.yonyougov.devops.fwc.core.controller.BaseController;
import com.yonyougov.devops.fwc.core.entity.Pagination;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by ligson on 2017/8/11.
 */
@Api(tags = "应用模块")
@RestController
@RequestMapping("/api/v2/app")
public class AppController extends BaseController {

    private final AppService appService;

    public AppController(AppService appService) {
        this.appService = appService;
    }


    //<!--用户自定义代码开始-->
    @ApiOperation(value = "前端:新增应用", notes = "新增应用")
    @PostMapping(value = "/a/add")
    public BaseResponseDto addApp(@RequestBody @Valid AddAppRequestDto addAppRequestDto) {
        AppEntity app = new AppEntity();
        BeanUtils.copyProperties(addAppRequestDto, app);
        appService.add(app);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "前端:删除应用", notes = "根据主键删除应用")
    @PostMapping(value = "/a/delete/id")
    public BaseResponseDto deleteAppById(@RequestBody @Valid AppDeleteRequestDto requestDto) {
        appService.deleteCascadeByPrimaryKey(requestDto.getId());
        return BaseResponseDto.getSuccessInstance();
    }


    @ApiOperation(value = "前端:修改应用", notes = "修改应用")
    @PostMapping(value = "/a/update")
    public BaseResponseDto updateApp(@RequestBody @Valid UpdateAppRequestDto updateAppRequestDto) {
        AppEntity appEntity = new AppEntity();
        BeanUtils.copyProperties(updateAppRequestDto, appEntity);
        appService.update(appEntity);
        return BaseResponseDto.getSuccessInstance();
    }

    @ApiOperation(value = "应用模块列表渲染调用", notes = "可选择根据应用名称进行模糊分页查询")
    @SneakyThrows
    @PostMapping(value = "/a/page/like")
    public BaseResponseDto findAppPageLike(@RequestBody @Valid AppPageLikeRequestDto request) {
        AppPageLikeDto appPageLikeDto = new AppPageLikeDto();
        BeanUtils.copyProperties(request, appPageLikeDto);
        Pagination<AppEntity> pagination = appService.findPageByRoleIds(appPageLikeDto);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("totalCount", pagination.getTotalCount());
        result.put("data", pagination.getDatas());
        return result;
    }

    @ApiOperation(value = "前端部署菜单新增部署信息时，渲染应用列表调用", notes = "当前用户已经关联的应用")
    @SneakyThrows
    @PostMapping(value = "/a/all/linked/userid")
    public BaseResponseDto findLinkApp() {
        LinkAppDto linkAppDto = new LinkAppDto();
        List<LinkAppVo> data = appService.findAppByRoleIds(linkAppDto);
        BaseResponseDto result = BaseResponseDto.getSuccessInstance();
        result.put("data", data);
        return result;
    }

    @ApiOperation(value = "角色管理-角色关联应用调用", notes = "根据角色id查询没有关联的应用列表")
    @PostMapping(value = "/a/all/nolink/roleid")
    public BaseResponseDto findNoLinkApp(@RequestBody @Valid NoLinkAppRequestDto request) {
        NoLinkAppByRoleIdDto noLinkAppByRoleIdDto = new NoLinkAppByRoleIdDto();
        BeanUtils.copyProperties(request, noLinkAppByRoleIdDto);

        Pagination<NoLinkAppVo> pagination = appService.findNoLinkAppPageByRoleId(noLinkAppByRoleIdDto);

        BaseResponseDto baseResponseDto = BaseResponseDto.getSuccessInstance();
        baseResponseDto.put("totalCount", pagination.getTotalCount());
        baseResponseDto.put("data", pagination.getDatas());
        return baseResponseDto;
    }

    //<!--用户自定义代码结束-->
}
